﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Security;
using NCS.IConnect.ApplicationContexts;
using Microsoft.SharePoint;
using System.Web;
using Microsoft.SharePoint.Administration;

namespace JM.JCY.SharePointComponent.UserManagement.UpdatePassword
{
    public partial class UpdatePasswordUserControl : UserControl
    {
        public string _userName
        {
            set { ViewState["_userName"] = value; }
            get { return (string)ViewState["_userName"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
       
        }

        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            string currentAccount = SPContext.Current.Web.CurrentUser.Name;
            MembershipUser membershipUser = MembershipProvider.GetUser(currentAccount, false);
           
            if (MembershipProvider.ChangePassword(currentAccount, this.txtCurrentPW.Text.Trim(), this.txtNewPW.Text.Trim()))
            {
                Response.Write("<Script>alert('密码修改成功，请牢记您的密码，下次请使用新密码登陆系统！');</script>");
               
            }
            else
            {
                Response.Write("<script>alert('密码修改失败，你确认您的老密码输入正确！');</script>");
        
            }
        }

      

        private static SPIisSettings _SPIisSettings;
        internal static SPIisSettings SPIisSettings
        {
            get
            {
                if (_SPIisSettings == null)
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        _SPIisSettings = SPContext.Current.Site.WebApplication.GetIisSettingsWithFallback(SPContext.Current.Site.Zone);
                    });
                }

                return _SPIisSettings;
            }
        }

        internal static MembershipProvider MembershipProvider
        {
            get
            {
                return Membership.Providers[SPIisSettings.FormsClaimsAuthenticationProvider.MembershipProvider];
            }
        }

        internal static RoleProvider RoleProvider
        {
            get
            {
                return Roles.Providers[SPIisSettings.FormsClaimsAuthenticationProvider.RoleProvider];
            }
        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("/pages/home.aspx");
        }
 
    }
}
